/* 
* sapdp-file.c
*
* Copyright (c) 2010, Zachary N J Peterson <znpeters@nps.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Naval Postgraduate School nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ZACHARY N J PETERSON ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZACHARY N J PETERSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "sapdp.h"

int sapdp_client_tag_file(char *filepath, size_t filepath_len, char *tagfilepath, size_t tagfilepath_len){
	
	SAPDP_key *key = NULL;
	SAPDP_client_tag *ctag = NULL;
	SAPDP_server_tag *stag = NULL;
	FILE *file = NULL;
	FILE *tagfile = NULL;
	unsigned int index = 0;
	char yesorno = 0;
	char realtagfilepath[MAXPATHLEN];
	char realtfilepath[MAXPATHLEN];
	unsigned char buf[SAPDP_BLOCK_SIZE];
	
	memset(realtagfilepath, 0, MAXPATHLEN);
	memset(realtfilepath, 0, MAXPATHLEN);

	if(!filepath) return 0;
	if(filepath_len >= MAXPATHLEN) return 0;
	if(tagfilepath_len >= MAXPATHLEN) return 0;
	
	/* If no tag file path is specified, add a .tag extension to the filepath */
	if(!tagfilepath && (filepath_len < MAXPATHLEN - 5)){
		if( snprintf(realtagfilepath, MAXPATHLEN, "%s.tag", filepath) >= MAXPATHLEN ) goto cleanup;
	}else{
		memcpy(realtagfilepath, tagfilepath, tagfilepath_len);
	}

	/* Check to see if the tag file exists */
	if( (access(realtagfilepath, F_OK) == 0) ){
		fprintf(stdout, "WARNING: A tag file for %s already exist; do you want to overwite (y/N)?", filepath);
		scanf("%c", &yesorno);
		if(yesorno != 'y') goto exit;
	}
	
	tagfile = fopen(realtagfilepath, "w");
	if(!tagfile){
		fprintf(stderr, "ERROR: Was not able to create %s.\n", realtagfilepath);
		goto cleanup;
	}
	
	/* Get the  keys */
	key = sapdp_generate_key();
	if(!key) goto cleanup;
	
	/* Open the file for reading */
	file = fopen(filepath, "r");
	if(!file){
		fprintf(stderr, "ERROR: Was not able to open %s for reading.\n", filepath);
		goto cleanup;
	}

	do{
		memset(buf, 0, SAPDP_BLOCK_SIZE);
		fread(buf, SAPDP_BLOCK_SIZE, 1, file);
		if(ferror(file)) goto cleanup;
		ctag = sapdp_client_tag_block(key, buf, SAPDP_BLOCK_SIZE, index);
		if(!ctag) goto cleanup;
		//if(!write_sapdp_client_tag(tagfile, ctag)) goto cleanup;
		index++;
		destroy_sapdp_client_tag(ctag);
		ctag = NULL;
	}while(!feof(file));	
	
exit:	
	if(key) sapdp_destroy_key(key);
	if(file) fclose(file);
	if(tagfile) fclose(tagfile);	
	return 1;	
	
cleanup:
	if(key) sapdp_destroy_key(key);
	if(ctag) destroy_sapdp_client_tag(ctag);
	if(stag) destroy_sapdp_server_tag(stag);
	if(file) fclose(file);
	if(tagfile){ 
		ftruncate(fileno(tagfile), 0);
		unlink(realtagfilepath);
		fclose(tagfile);
	}
	return 0;
}